package com.zp.framework.services.test;

import javax.ws.rs.core.MediaType;

import junit.framework.Assert;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.zp.bean.common.ResultContentBaseVo;
import com.zp.bean.common.ResultContentVo;

/**
 * test News services function with Junit
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/3/1
 * 
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:configure/applicationContext.xml" })
public class RunContentServices {

	private static final Log logger = LogFactory
			.getLog(RunContentServices.class);
	private static final String URI = "http://localhost:8082/ZP/rest";
	private WebResource service;

	@Before
	public void init() {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		service = client.resource(URI);
		logger.info("*********** testing to initial services client ****************");
	}

	@After
	public void destory() {
		service = null;
		logger.info("*********** testing to destory services instance ******************");
	}

	/**@Test**/
	public void testfindNewsById() {
		ClientResponse response = service.path("news/getNews")
				.queryParam("newsId", "3").post(ClientResponse.class);
		ResultContentVo result = response.getEntity(ResultContentVo.class);
		Assert.assertNotNull(result.getContent());
	}


	@Test
	public void testSearchNewsByType() {
		ClientResponse response = service.path("news/searchNews")
				.queryParam("pageNo", "1").queryParam("pageSize", "10").queryParam("value", "cm02").queryParam("top", "1").type(MediaType.APPLICATION_JSON_TYPE).post(ClientResponse.class);
		ResultContentBaseVo result=response.getEntity(ResultContentBaseVo.class);
		Assert.assertEquals(3, result.getTotalCount());
	}
	/**@Test**/
	public void testSearchTopNews() {
		ClientResponse response = service.path("news/searchTopNews")
				.queryParam("number", "3").queryParam("typeCode", "cm04").post(ClientResponse.class);
		ResultContentBaseVo result=response.getEntity(ResultContentBaseVo.class);
		Assert.assertEquals(3, result.getTotalCount());
	}
}
